package com.cardbaobao.cardbabyclient.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.cardbaobao.cardbabyclient.model.UserInfo;

public class SharedPreferencesUtil {
    public static class CbbInfo{
        private static final String SP_FILE_NAME ="cbbInfo";
        /**是否是新装用户*/
        private static final String KEY_FIRST_START="first_start";
        /**服务器上app最新apk包下载地址*/
        private static final String KEY_APP_LATEST_APK_URL="service_apk_url";
        /**服务器上app最新版本号*/
        private static final String KEY_APP_LATEST_VERSION_CODE="service_version_code";
        /**最近一次关闭弹窗广告的时间*/
        private static final String KEY_LATEST_CLOSE_AD_POP_TIME = "first_close_ad_pop_time";
        /**获取照片回调存放标识*/
        private static final String KEY_GET_PICTURE_PATH = "KEY_GET_PICTURE_PATH";
        /**旧版本遗留 勿动!!!*/
        public static final String KEY_PHOTO="cardbaobao/images/screenshots";

        public static boolean isAppBeUsed(){
            //旧版本使用的是String类型!!!启动过后的值为true
            return "true".equals(getString(SP_FILE_NAME,KEY_FIRST_START));
        }

        public static void setAppBeUsed(){
            //旧版本使用的是true表示已经启动过
            setString(SP_FILE_NAME,KEY_FIRST_START,"true");
        }

        public static void setAppUpdateInfo(int versionCode,@NonNull String apkUrl){
            setInt(SP_FILE_NAME, KEY_APP_LATEST_VERSION_CODE,versionCode);
            setString(SP_FILE_NAME,KEY_APP_LATEST_APK_URL,apkUrl);
        }

        public static int getAppLatestVersionCode(){
            return getInt(SP_FILE_NAME,KEY_APP_LATEST_VERSION_CODE);
        }

        public static String getAppLatestApkUrl(){
            return getString(SP_FILE_NAME,KEY_APP_LATEST_APK_URL);
        }

        public static void setLatestCloseAdPopTime(long millisecond){
            setLong(SP_FILE_NAME,KEY_LATEST_CLOSE_AD_POP_TIME,millisecond);
        }

        public static long getLatestCloseAdPopTime(){
            return getLong(SP_FILE_NAME,KEY_LATEST_CLOSE_AD_POP_TIME);
        }

        public static String getPicturePath(){
            return getString(SP_FILE_NAME,KEY_GET_PICTURE_PATH);
        }

        public static void setPicturePath(){

        }

        public static String getPhoto(){
            return getString(SP_FILE_NAME,KEY_PHOTO);
        }

        public static void setPhoto(String photo){
            setString(SP_FILE_NAME,KEY_PHOTO,photo);
        }
    }

    public static class LocationInfo{
        private static final String SP_FILE_NAME ="locationInfo";
        /** 存储当前省份的ID标识*/
        private static final String KEY_PROVINCE_ID = "key_province_id";
        /**存储当前城市的标识*/
        private static final String KEY_CITY = "key_city";
        /**存储当前城市ID的标识*/
        private static final String KEY_CITY_ID = "key_city_id";
        /**存储当前城市的经度标识*/
        private static final String KEY_CITY_LONGITUDE = "key_city_longitude";
        /**存储当前城市的纬度标识*/
        private static final String KEY_CITY_LATITUDE = "key_city_latitude";
        /**24小时第一次定位时间*/
        private final static String KEY_24_FIRST_LOCATION = "first_24_location";

        public static String getProvinceId(){
            return getString(SP_FILE_NAME,KEY_PROVINCE_ID);
        }

        public static void setProvinceId(String provinceId){
            setString(SP_FILE_NAME,KEY_PROVINCE_ID,provinceId);
        }

        public static String getCityName(){
            return getString(SP_FILE_NAME,KEY_CITY);
        }

        public static void setCityName(String cityName){
            setString(SP_FILE_NAME,KEY_CITY,cityName);
        }

        public static String getCityId(){
            return getString(SP_FILE_NAME,KEY_CITY_ID);
        }

        public static void setCityId(String cityID){
            setString(SP_FILE_NAME,KEY_CITY_ID,cityID);
        }

        public static String getCityLongitude(){
            return getString(SP_FILE_NAME,KEY_CITY_LONGITUDE);
        }

        public static void setCityLongitude(String longitude){
            setString(SP_FILE_NAME,KEY_CITY_LONGITUDE,longitude);
        }

        public static String getCityLatitude(){
            return getString(SP_FILE_NAME,KEY_CITY_LATITUDE);
        }

        public static void setCityLatitude(String latitude){
            setString(SP_FILE_NAME,KEY_CITY_LATITUDE,latitude);
        }

        public static long get24FirstLocation(){
            return getLong(SP_FILE_NAME,KEY_24_FIRST_LOCATION);
        }

        public static void set24FirstLocation(long time){
            setLong(SP_FILE_NAME,KEY_24_FIRST_LOCATION,time);
        }
    }

    public static class CbbUserInfo{
        private static final String SP_FILE_NAME ="cbbUserInfo";
        private static final String KEY_PHONE="phone";
        private static final String KEY_ID="id";
        private static final String KEY_USER_NAME="user_name";
        private static final String KEY_CITY="city";
        private static final String KEY_AVATAR="avatar";
        private static final String KEY_ID_CARD_NO="id_card_no";
        private static final String KEY_TRUE_NAME="true_name";

        public static UserInfo getUserInfo(){
            UserInfo userInfo=new UserInfo();
            userInfo.setID(getInt(SP_FILE_NAME,KEY_ID));
            userInfo.setUserName(getString(SP_FILE_NAME,KEY_USER_NAME));
            userInfo.setPhone(getString(SP_FILE_NAME,KEY_PHONE));
            userInfo.setCity(getString(SP_FILE_NAME,KEY_CITY));
            userInfo.setHeadImages(getString(SP_FILE_NAME,KEY_AVATAR));
            userInfo.setTrueName(getString(SP_FILE_NAME,KEY_TRUE_NAME));
            userInfo.setIDCardNo(getString(SP_FILE_NAME,KEY_ID_CARD_NO));
            return userInfo;
        }

        public static void setUserInfo(UserInfo userInfo){
            setInt(SP_FILE_NAME,KEY_ID,userInfo.getID());
            setString(SP_FILE_NAME,KEY_USER_NAME,userInfo.getUserName());
            setString(SP_FILE_NAME,KEY_PHONE,userInfo.getPhone());
            setString(SP_FILE_NAME,KEY_CITY,userInfo.getCity());
            setString(SP_FILE_NAME,KEY_AVATAR,userInfo.getHeadImages());
            setString(SP_FILE_NAME,KEY_TRUE_NAME,userInfo.getTrueName());
            setString(SP_FILE_NAME,KEY_ID_CARD_NO,userInfo.getIDCardNo());
        }
    }

    public static class PhoneInfo{
        private static final String SP_FILE_NAME ="phoneInfo";
        //游客id，请求接口保存(服务端随机生成)
        private static final String KEY_TEMPORARY_ID="temporary_id";

        public static String getTemporaryId(){
            return getString(SP_FILE_NAME,KEY_TEMPORARY_ID);
        }

        public static void setTemporaryId(String temporaryId){
            setString(SP_FILE_NAME,KEY_TEMPORARY_ID,temporaryId);
        }
    }

    public static class ConfigInfo{
        private static final String SP_FILE_NAME ="configInfo";
        //测试地址类型
        private static final String KEY_API_ADDRESS_TYPE="key_api_address_type";

        /**
         * 获取测试地址类型
         * @return 1、测试环境一 2、测试环境二 3、灰度环境 4、生产环境 其他为开发环境(debug)
         */
        public static int getApiAddressType(){
            return getInt(SP_FILE_NAME,KEY_API_ADDRESS_TYPE);
        }

        /**
         * 设置测试地址类型
         * @param type 1、测试环境一 2、测试环境二 3、灰度环境 4、生产环境 其他为开发环境(debug)
         */
        public static void setApiAddressType(int type){
            setInt(SP_FILE_NAME,KEY_API_ADDRESS_TYPE,type);
        }
    }

    private static boolean getBoolean(String spFileName,String key){
        SharedPreferences sp=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        return sp.getBoolean(key,false);
    }

    private static String getString(String spFileName,String key){
        SharedPreferences sp=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE);
        return sp.getString(key,null);
    }

    private static void setBoolean(String spFileName,String key,boolean value){
        SharedPreferences.Editor editor=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE).
                        edit();
        editor.putBoolean(key,value);
        editor.commit();
    }

    private static void setString(String spFileName,String key,String value){
        SharedPreferences.Editor editor=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE).
                        edit();
        editor.putString(key, TextUtils.isEmpty(value)?"":value);
        editor.commit();
    }

    private static int getInt(String spFileName,String key){
        SharedPreferences sp=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE);
        return sp.getInt(key,0);
    }

    private static void setInt(String spFileName,String key,int value){
        SharedPreferences.Editor editor=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE).
                        edit();
        editor.putInt(key,value);
        editor.commit();
    }

    private static long getLong(String spFileName,String key){
        SharedPreferences sp=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE);
        return sp.getLong(key,0L);
    }

    private static void setLong(String spFileName,String key,long value){
        SharedPreferences.Editor editor=
                ApplicationHelper.getApplicationContext().
                        getSharedPreferences(spFileName,Context.MODE_PRIVATE).
                        edit();
        editor.putLong(key,value);
        editor.commit();
    }
}